﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Web.Script.Serialization;
using System.IO;

namespace INNOVACALL.Framework.Common.POCOT4.MVCViewJSObjects
{
    public class JQClass
    {
        private string arg;
        private string sAssemblyName;
        private Assembly assem;
        private Type[] types;

        public Type[] GetTypes()
        {
            return types;
        }

        public JQClass(string sAssemblyName)
        {
            assem = Assembly.LoadFrom(sAssemblyName);
            types = assem.GetTypes();
        }

        public Object CreateInstance(Type type)
        {
            Object myObject = Activator.CreateInstance(type);
            return myObject;
        }

        public List<Type> ObtientListTypesMeta(List<string> ListNom)
        {
            List<Type> listMetaTypes = new List<Type>();
            foreach (string nom in ListNom)
            {
                listMetaTypes.Add(types.ToList().Find(p => p.Name == nom));
            }
            return listMetaTypes;
        }

        public List<string> ObtientListChaineJS(List<string> lstName)
        {
	        List<Type> lstTypes = new List<Type>();
	        lstTypes = this.ObtientListTypesMeta(lstName);
	
	        List<string> lstObjetJS = new List<string>();
	        foreach(Type typeCourant in lstTypes)
	        {
		        string jqclassMetaData = typeCourant.Name;
		        object metaData = this.CreateInstance(typeCourant);

		        JavaScriptSerializer serializer = new JavaScriptSerializer();
	            serializer.MaxJsonLength = Int32.MaxValue;
	        
		        string objetjson = serializer.Serialize(metaData).ToString();

		        string chaineJS = "var " + jqclassMetaData.ToLowerInvariant() + " = " + objetjson;
		        lstObjetJS.Add(chaineJS);
	        }
            return lstObjetJS;
        }
    }
}
